package net.stogi.dsper.ui.client.module.datasource;

import java.util.HashMap;
import java.util.Map;

import net.stogi.common.gwt.dispatch.client.core.ActionManager;
import net.stogi.common.gwt.frame.client.core.EventBus;
import net.stogi.common.gwt.frame.client.module.Module;
import net.stogi.common.gwt.frame.client.mvp.AbstractPresenter;

import com.google.gwt.user.client.ui.Widget;

public class DataSourceModule implements Module {

	public static final String DATA_SOURCE_EDITOR_PRESENTER = "data-source-editor-presenter";

	public static final String DATA_SOURCE_EDITOR_WIDGET = "data-source-editor-widget";

	public static final String DATA_SOURCE_LIST_PRESENTER = "data-source-list-presenter";

	public static final String DATA_SOURCE_LIST_WIDGET = "data-source-list-widget";

	public static final String DATA_SOURCE_SELECTOR_PRESENTER = "data-source-selector-presenter";

	public static final String DATA_SOURCE_SELECTOR_WIDGET = "data-source-selector-widget";

	private ActionManager actionManager;

	private EventBus eventBus;

	private Map<String, AbstractPresenter> presenterMap = new HashMap<String, AbstractPresenter>();

	private Map<String, Widget> widgetMap = new HashMap<String, Widget>();

	public DataSourceModule(ActionManager actionManager, EventBus eventBus) {
		this.actionManager = actionManager;
		this.eventBus = eventBus;
		initModule();
	}

	@Override
	public Widget getWidget(String name) {
		return widgetMap.get(name);
	}

	protected void initModule() {
		DataSourceEditorWidget dataSourceEditorWidget = new DataSourceEditorWidget();
		DataSourceEditorPresenter dataSourceEditorPresenter = new DataSourceEditorPresenter(dataSourceEditorWidget,
				actionManager, eventBus);
		DataSourceListWidget dataSourceListWidget = new DataSourceListWidget();
		DataSourceListPresenter dataSourceListPresenter = new DataSourceListPresenter(dataSourceListWidget, actionManager,
				eventBus);
		DataSourceSelectorWidget dataSourceSelectorWidget = new DataSourceSelectorWidget();
		DataSourceSelectorPresenter dataSourceSelectorPresenter = new DataSourceSelectorPresenter(dataSourceSelectorWidget,
				actionManager, eventBus);
		// Register widgets and presenters.
		widgetMap.put(DATA_SOURCE_EDITOR_WIDGET, dataSourceEditorWidget);
		presenterMap.put(DATA_SOURCE_EDITOR_PRESENTER, dataSourceEditorPresenter);
		widgetMap.put(DATA_SOURCE_LIST_WIDGET, dataSourceListWidget);
		presenterMap.put(DATA_SOURCE_LIST_PRESENTER, dataSourceListPresenter);
		widgetMap.put(DATA_SOURCE_SELECTOR_WIDGET, dataSourceSelectorWidget);
		presenterMap.put(DATA_SOURCE_SELECTOR_PRESENTER, dataSourceSelectorPresenter);
	}
}
